package com.runfast.waimai.web.controller.goods;

import com.runfast.common.web.entity.Result;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.RunfastBusinessService;
import com.runfast.waimai.service.RunfastGoodsSellService;
import com.runfast.waimai.service.RunfastGoodsSellTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ${DESCRIPTION} 外卖商品分类
 *
 * @author chenzheng
 * @create 2018-05-08 19:56
 **/
@RestController("goodsSellTypeController")
@RequestMapping("/server/goodsSellType")
public class GoodsSellTypeController {
    @Autowired
    private RunfastGoodsSellTypeService runfastGoodsSellTypeService;
    @Autowired
    private RunfastBusinessService runfastBusinessService;
    @Autowired
    private RunfastGoodsSellService runfastGoodsSellService;

    /**
     * 商品分类列表带分页的
     */
    @GetMapping("/list")
    //@RequiresPermissions("server:goodsSellType:list")
    public Result list(@PageableDefault Pageable pageable, Map<String, Object> params){

        RunfastGoodsSellTypeExample example=new RunfastGoodsSellTypeExample();
        List<RunfastGoodsSellType> list=runfastGoodsSellTypeService.selectByExampleWithPageable(example,pageable);
        Integer total=runfastGoodsSellTypeService.countByExampleWithPageable(example,pageable);
        Map<Object,Object> map=new HashMap<Object, Object>();
        map.put("list",list);
        map.put("total",total);
        return Result.ok(null,map);
    }

    /**
     * 商家对应的---商品分类列表
     */
    @GetMapping("/select")
    public Result select(Integer businessId){
        if(businessId==null){return Result.fail(null,"商家id不能为空！");}
        RunfastBusiness business=runfastBusinessService.selectByPrimaryKey(businessId);
        RunfastGoodsSellTypeExample example=new RunfastGoodsSellTypeExample();
        example.createCriteria().andBusinessidEqualTo(business.getId()).andAgentidEqualTo(business.getAgentid()).andDeletedNotEqualTo(1);
        example.setOrderByClause("sort asc");
        List<RunfastGoodsSellType> list = runfastGoodsSellTypeService.selectByExample(example);
        return Result.ok(null,list);
    }

    /**
     * 保存商品分类
     */
    @PostMapping("/save")
    public Result save(@RequestBody RunfastGoodsSellType type){
        if(type==null){return Result.fail(null,"内容不能为空！");}
        if(type.getBusinessid()==null){return Result.fail(null,"商家id不能为空！");}
        RunfastBusiness business=runfastBusinessService.selectByPrimaryKey(type.getBusinessid());
        if(business==null){return Result.fail(null,"找不到对应的商家！");}
        type.setAgentid(business.getAgentid());
        type.setAgentname(business.getAgentname());
        type.setCreatetime(new Date());
        type.setDeleted(0);
        RunfastGoodsSellTypeExample example=new RunfastGoodsSellTypeExample();
        example.createCriteria().andAgentidEqualTo(business.getAgentid()).andBusinessidEqualTo(business.getId());
        Integer total=runfastGoodsSellTypeService.countByExample(example);
        if(total==0){
            type.setSort(0);
        }else{
            type.setSort(total);
        }
        runfastGoodsSellTypeService.insertSelective(type);
        return Result.ok(null);
    }

    /**
     * 修改商品分类
     */
    @PostMapping("/update")
    //@RequiresPermissions("server:goodsSellType:update")
    public Result update(@RequestBody RunfastGoodsSellType type){
        if(type==null){return Result.fail(null,"修改的商品分类信息不能为空！");}
        runfastGoodsSellTypeService.updateByPrimaryKeySelective(type);
        return Result.ok(null);
    }

    /**
     * 删除商品分类
     */
    @GetMapping("/delete")
    public Result delete(Integer id){
        if(id!=null){
            RunfastGoodsSellType type=runfastGoodsSellTypeService.selectByPrimaryKey(id);
            if(type!=null) {
                RunfastGoodsSellExample example = new RunfastGoodsSellExample();
                example.createCriteria().andSelltypeidEqualTo(type.getId()).andDeletedNotEqualTo(1);
                Integer total = runfastGoodsSellService.countByExample(example);
                if (total > 0) {
                    return Result.fail(null, "分类正在被使用，不能删除！");
                } else {
                    type.setDeleted(1);
                    runfastGoodsSellTypeService.updateByPrimaryKeySelective(type);
                }
            }
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null);
    }


    /**拖动排序*/
    @PostMapping("/dragSort")
    public Result dragSort(@RequestBody RunfastGoodsSellType type){
        if(type!=null && type.getIds()!=null){
            int i=0;
            for(Integer in:type.getIds()){
                RunfastGoodsSellType type1=new RunfastGoodsSellType();
                type1.setId(in);
                type1.setSort(i);
                runfastGoodsSellTypeService.updateByPrimaryKeySelective(type1);
                i++;
            }
        }
        return Result.ok(null);
    }

}
